<?php
/**
 * This file is part of League of Legends PHP Inibin Parser.
 *
 * League of Legends PHP Inibin Parser is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * League of Legends PHP Inibin Parser is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with League of Legends PHP Inibin Parser.  If not, see <http://www.gnu.org/licenses/>.
 */


/**
 * League of Legends Client-Database parser
 *
 * @author Marcel Klimek <klimek@secondred.de>
 * @copyright Marcel Klimek
 * @version $Id: copycharbin.php 3 2011-11-12 10:07:44Z naffto88@gmail.com $
 */

/**
 * This PHP-File was just for me to copy all character *.inibin files out of the dumped folder
 * from the RAF Manager
 * 
 * I dumped it like following:
 * - loldata
 *      - 0.0.0.25
 *          - DATA
 *              - Characters
 *              - ...
 *      - 0.0.0.26
 *          - DATA
 *              - Characters
 *              - ...
 *      - 0.0.0.27
 *      - ....
 * 
 * if you use this folder structure you can use this method here
 * with this method i copy all *.inibin files out of the Characters folder
 * and copy them into a seperate Folder named "chardata" with the same folder
 * structure, created by this script
 */


/**
 * This method will recursive search for all $strSearchFiles
 * 
 * @param type $strVerzeichnis folder where to search
 * @param type $strSearchFolder folder where to search within the folder nr 1
 * @param type $strSearchFile filenames to search for e.g. ".inibin"
 * @return array Array of all Filepaths with $strSearchFile in it
 */
function recursiveDir($strVerzeichnis,$strSearchFolder,$strSearchFile)
{
    $arrFiles = array();
    $handle =  opendir($strVerzeichnis);
    while ($datei = readdir($handle))
    {
        if ($datei != "." && $datei != "..")
        {   //if it is an directory && ($dat is the searched folder (e.g. 'Characters') || it is the version folder(e.g. 0.0.0.25) || or it is the DATA folder or an part of $strSearchFolder is in the filepath)
            if (is_dir($strVerzeichnis. DIRECTORY_SEPARATOR .$datei) && ($datei == $strSearchFolder || strpos($datei, "0.0.0.") !== FALSE || $datei == "DATA" || strpos($strVerzeichnis, $strSearchFolder) !== FALSE))
            {
                $arrFiles = array_merge($arrFiles,recursiveDir($strVerzeichnis. DIRECTORY_SEPARATOR .$datei,$strSearchFolder,$strSearchFile));
            } else
            {
                if (strpos($datei, $strSearchFile) !== FALSE)
                {
                    $arrFiles[] = $strVerzeichnis. DIRECTORY_SEPARATOR .$datei;
                }
            }
        }
    }
    closedir($handle);

    return $arrFiles;
}

$strFromFolder = "loldata";

$arrFiles = recursiveDir($strFromFolder, "Characters",".inibin");
var_dump($arrFiles);


$strToFolder = "chardata";

//now copy all found files
foreach ($arrFiles AS $key => $strStructure)
{
    $strNewLoc = str_replace($strFromFolder,$strToFolder,$strStructure);
    
    //create the folder if it doesnt exist
    $arrFolders = explode('\\',$strNewLoc);
    foreach ($arrFolders AS $key => $strFolder)
    {
        if (strpos($strFolder, ".inibin") !== FALSE)
        {
            
        } else
        {
            $i = 0;
            $strPath = "";
            while ($i < $key)
            {
                //building the path
                $strPath .= $arrFolders[$i] . DIRECTORY_SEPARATOR;
                $i++;
            }
            if (!is_dir($strPath . $strFolder))
            {
                mkdir ($strPath . $strFolder);
            }
        }
    }
    
    copy($strStructure, $strNewLoc);
}
?>
